package com.talkweb.ncfw.web.action;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.beanutils.BeanUtils;

import com.talkweb.ncfw.component.rights.entity.Menu;
import com.talkweb.ncfw.component.rights.service.IMenuManager;
import com.talkweb.ncfw.data.DataMap;
import com.talkweb.ncfw.data.IDataMap;
import com.talkweb.ncfw.data.Page;
import com.talkweb.ncfw.data.ProcessResult;
import com.talkweb.ncfw.exceptions.BizBaseException;
import com.talkweb.ncfw.exceptions.ViewBaseException;
import com.talkweb.ncfw.security.SecurityHelper;
import com.talkweb.ncfw.utils.CollectionUtils;
import com.talkweb.ncfw.utils.StringUtils;
import com.talkweb.ncfw.web.struts2.ExtGridBaseAction;
import com.talkweb.ncfw.web.utils.HttpServletUtils;
import com.talkweb.ncfw.web.utils.tree.TreeBean;
import com.talkweb.ncfw.web.utils.tree.TreeUtils;

/**
 * <p>文件名称: MenuAction.java</p>
 * <p>文件描述: 系统菜单Action</p>
 * <p>版权所有: 版权所有(C)2010</p>
 * <p>公　　司: 拓维信息系统股份有限公司</p>
 * <p>内容摘要: 简要描述本文件的内容，包括主要模块、函数及能的说明</p>
 * <p>其他说明: 其它内容的说明</p>
 * <p>完成日期: 2011-5-3</p>
 * <p>修改记录1:</p>
 * <pre>
 *    修改日期:
 *    修 改 人:
 *    修改内容:
 * </pre>
 * <p>修改记录2：…</p>
 * @author  Wuqingming
 */
public class MenuAction extends ExtGridBaseAction {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1599650330892032910L;
	
	private IMenuManager menuManager;
	
	public IMenuManager getMenuManager() {
		return menuManager;
	}

	public void setMenuManager(IMenuManager menuManager) {
		this.menuManager = menuManager;
	}

	/**
	 * 
	 * @author：Wuqingming 	        
	 * @date：2011-9-19
	 * @Description：获取菜单列表
	 * @return
	 * @throws ViewBaseException
	 */
	public String listMenu () throws ViewBaseException {
		Page<Menu> page = this.getPage();
		IDataMap<String, Object> condition = this.getParameterMap(); 
		try {
			page = this.menuManager.findMenu(page, condition);
		} catch (BizBaseException e) {
			throw new ViewBaseException(e.getMessage(), e);
		}
		if (HttpServletUtils.isAjaxRequest(this.getRequest())) {
			this.bindDataGrid(page);
		}
		return null;
	}
	
	/**
	 * 
	 * @author：Wuqingming 	        
	 * @date：2011-9-19
	 * @Description：录入菜单信息
	 * @return
	 * @throws ViewBaseException
	 */
	public String inputMenu () throws ViewBaseException {
		boolean flag = false;
		String msg = null;
		int returnCode = 0;
		IDataMap<String, Object> conditions = this.getParameterMap();
		Menu menu = new Menu();
		try {
			BeanUtils.copyProperties(menu, conditions);
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		}
		if (StringUtils.isEmpty(menu.getMenuId())) {//新增菜单
			try {
				returnCode = this.menuManager.addMenu(menu);
			} catch (BizBaseException e) {
				throw new ViewBaseException(e.getMessage(), e);
			}
			flag = (returnCode == 1);
			msg = flag ? "菜单添加成功" : "菜单添加失败";
		} else {//修改菜单
			try {
				returnCode = this.menuManager.updateMenu(menu);
			} catch (BizBaseException e) {
				throw new ViewBaseException(e.getMessage(), e);
			}
			flag = (returnCode == 1);
			msg = flag ? "菜单信息修改成功" : "菜单信息修改失败";
		}
		ProcessResult result = new ProcessResult(flag, msg);
		if (HttpServletUtils.isAjaxRequest(this.getRequest())) {
			HttpServletUtils.outJson(this.getResponse(), result);
		}
		return null;
	}
	
	/**
	 * 
	 * @author：Wuqingming 	        
	 * @date：2011-9-19
	 * @Description：删除菜单
	 * @return
	 * @throws ViewBaseException
	 */
	public String deleteMenu () throws ViewBaseException {
		boolean flag = false;
		String msg = null;
		int returnCode = 0;
		String menuIds = this.getParameter("menuIds");
		String[] menuIdArray = null;
		if (!StringUtils.isEmpty(menuIds)) {
			menuIdArray = menuIds.split(",");
		}
		try {
			returnCode = this.menuManager.deleteMenuByMenuId(menuIdArray);
			flag = (returnCode == 1);
			msg = flag ? "菜单删除成功" : "菜单删除失败";
		} catch (BizBaseException e) {
			throw new ViewBaseException(e.getMessage(), e);
		}
		ProcessResult result = new ProcessResult(flag, msg);
		if (HttpServletUtils.isAjaxRequest(this.getRequest())) {
			HttpServletUtils.outJson(this.getResponse(), result);
		}
		return null;
	}

	/**
	 * 
	 * @author：Wuqingming 	        
	 * @date：2011-5-3
	 * @Description：获取单个菜单的详细信息
	 * @return
	 */
	public String getMenuByMenuId () throws ViewBaseException {
		String menuId = this.getParameter("menuId");
		Menu menu = null;
		try {
			menu = this.menuManager.getMenuByMenuId(menuId);
		} catch (BizBaseException e) {
			throw new ViewBaseException(e.getMessage(), e);
		}
		if (HttpServletUtils.isAjaxRequest(this.getRequest())) {
			HttpServletUtils.outJson(this.getResponse(), menu);
		}
		return null;
	}
	
	/**
	 * 
	 * @author：Wuqingming 	        
	 * @date：2011-5-12
	 * @Description：获取子菜单节点
	 * @param request
	 * @param response
	 * @return
	 */
	public String findSubMenuNodes() throws ViewBaseException {
		String menuId = this.getParameter("node");
		Page<Menu> page = new Page<Menu>();
		page.setAutoCount(false);
		page.setIfPage(false);
		page.setOrderBy("displayOrder");
		page.setOrder(Page.ASC);
		try {
			page = this.menuManager.findSubMenuByMenuId(page, menuId);
		} catch (BizBaseException e) {
			throw new ViewBaseException(e.getMessage(), e);
		}
		List<Menu> menuList = page.getResult();
		if (menuList == null || menuList.size() == 0) {
			return null;
		}
		List<TreeBean> treeList = new ArrayList<TreeBean>(menuList.size());
		boolean isLeaf;
		for (Menu menu : menuList) {
			TreeBean treeBean = new TreeBean();
			treeBean.setId(menu.getMenuId());
			treeBean.setText(menu.getMenuLabelName());
			treeBean.setParentid(menu.getParentMenuId());
			treeBean.setExpanded(false);
			isLeaf = "y".equals(menu.getIsLeaf()) ? true : false;
			treeBean.setLeaf(isLeaf);
			Map<String, Object> attributes = new HashMap<String, Object>();
			attributes.put("id", menu.getMenuId());
			treeBean.setAttributes(attributes);
			treeList.add(treeBean);
		}
		HttpServletUtils.outJsonArray(this.getResponse(), treeList);
		return null;
	}
	
	/**
	 * 
	 * @author：Wuqingming 	        
	 * @date：2011-9-19
	 * @Description：查找菜单树菜单，并支持三态树
	 * @return
	 * @throws ViewBaseException
	 */
	public String findMenuSupportTree () throws ViewBaseException {
		//请求系统菜单数据
		IDataMap<String, Object> condition = new DataMap<String, Object>();
		Page<Menu> page = new Page<Menu>();
		page.setAutoCount(false);
		page.setIfPage(false);
		page.setOrderBy("displayOrder");
		page.setOrder(Page.ASC);
		String treeType = this.getParameter("treeType");
		try {
			page = this.menuManager.findMenu(page, condition);
		} catch (BizBaseException e) {
			throw new ViewBaseException(e.getMessage(), e);
		}
		List<Menu> menuList = page.getResult();
		if (menuList == null || menuList.size() == 0) {
			return null;
		}
		List<TreeBean> treeBeanList = new ArrayList<TreeBean>();
		for (Menu menu : menuList) {
			TreeBean treeBean = new TreeBean();
			treeBean.setId(menu.getMenuId());
			treeBean.setParentid(menu.getParentMenuId());
			treeBean.setText(menu.getMenuLabelName());
			if (StringUtils.equals(treeType, "tree3")) {
				treeBean.setUiProvider("checkBox");
				treeBean.setExpanded(true);
			}
			treeBeanList.add(treeBean);
		}
		List<TreeBean> treeList = TreeUtils.onTree(treeBeanList, "", "");
		HttpServletUtils.outJsonArray(this.getResponse(), treeList);
		return null;
	}
	
	/**
	 * 
	 * @author：Wuqingming 	        
	 * @date：2011-10-8
	 * @Description：创建菜单导航
	 * @return
	 * @throws ViewBaseException
	 */
	public String createMenuNav () throws ViewBaseException {
		String[] roleIdArray = SecurityHelper.getCurrentUserRoles();
		Page<Menu> page = new Page<Menu>();
		page.setAutoCount(false);
		page.setIfPage(false);
		page.setOrderBy("displayOrder");
		page.setOrder(Page.ASC);
		try {
			page = this.menuManager.findMenuInRoles(page, roleIdArray, null);
		} catch (BizBaseException e) {
			throw new ViewBaseException(e.getMessage(), e);
		}
		List<Menu> menuList = page.getResult();
		if (menuList == null || menuList.size() == 0) {
			return null;
		}
		if (CollectionUtils.isEmpty(menuList)) {
			return null;
		}
		List<TreeBean> treeBasicList = 
			new ArrayList<TreeBean>(menuList.size());
		for (Menu menu : menuList) {
			TreeBean treeBasic = new TreeBean();
			treeBasic.setId(menu.getMenuId());
			treeBasic.setText(menu.getMenuLabelName());
			treeBasic.setParentid(menu.getParentMenuId());
			treeBasic.setHrefTarget(menu.getOpenMode());
			DataMap<String, Object> attributes = new DataMap<String, Object>();
			attributes.put("action", menu.getMenuEntry());
			treeBasic.setAttributes(attributes);
			//treeBasic.setHref(menu.getMenuEntry());
			treeBasicList.add(treeBasic);
		}
		List<TreeBean> treeBeanList = TreeUtils.onTree(treeBasicList, "", "");
		HttpServletUtils.outJsonArray(this.getResponse(), treeBeanList);
		return null;
	}
	
	/**
	 * 
	 * @author：Wuqingming 	        
	 * @date：2011-10-8
	 * @Description：查找菜单
	 * @return
	 * @throws ViewBaseException
	 */
	public String findMenu () throws ViewBaseException {
		String[] roleIdArray = SecurityHelper.getCurrentUserRoles();
		Page<Menu> page = new Page<Menu>();
		page.setAutoCount(false);
		page.setIfPage(false);
		page.setOrderBy("displayOrder");
		page.setOrder(Page.ASC);
		try {
			page = this.menuManager.findMenuInRoles(page, roleIdArray, null);
		} catch (BizBaseException e) {
			throw new ViewBaseException(e.getMessage(), e);
		}
		List<Menu> menuList = page.getResult();
		HttpServletUtils.outJsonArray(this.getResponse(), menuList);
		return null;
	}
	
}

